
//  Created by 莫瑞权 on 2022/5/10.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFMediaContainerView: TalkfunHitTestView {

    ////5 mp3     4 mp4
    @objc public var docType:NSInteger = 0
    @objc public let playVIew = UIView()
    @objc public let bottomBackground = UIView()
    @objc public let doubleTapActionBlock:touchesBeganBlock? = nil
    //是否应用了
    @objc public var isMediaApplicatea:Bool = false
    @objc public var tempFrame:CGRect = CGRect(x: 0, y: 0, width: 0, height: 0)
    //mp3 时用到
    @objc public var pageFrame:CGRect = CGRect(x: 0, y: 0, width: 0, height: 0)
    //全屏
    @objc public let fullScreen = TalkfunIncreaseButton()

     //总时长
    @objc public var duration:NSInteger = 0
    @objc public let timeLabel = UILabel()
    @objc public var status:TalkfunMultiMediaStatusChangeListener = TalkfunMultiMediaStatusChangeListenerStop
    @objc public let mp3Image = UIImageView()
    //进度
    @objc public let program = UIView()
    //总时长
    @objc public let durationView = UIView()
    @objc public var tempTime:CGFloat = 0
    @objc public var rotationAnimation = CABasicAnimation.init(keyPath: "transform.rotation.z")

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(self.playVIew)
        let doubleTap = UITapGestureRecognizer.init(target: self, action: #selector(self.doubleTapAction))
      
        doubleTap.numberOfTapsRequired = 2
        self.addGestureRecognizer(doubleTap)
        
        //进入前台时调用
        NotificationCenter.default.addObserver(self, selector: #selector(self.onUIApplicationWillEnterForegroundNotification), name: UIApplication.didBecomeActiveNotification, object: nil)
       
        self.bottomBackground.isHidden = true
//        self.bottomBackground.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.7)
        self.bottomBackground.backgroundColor = UIColor.red
        self.playVIew.addSubview(self.bottomBackground)
        
        self.mp3Image.backgroundColor = UIColor.clear
        self.bottomBackground.addSubview(self.mp3Image)
        
        //TODO:时间格式
        self.timeLabel.font = UIFont.systemFont(ofSize: 14)
        self.bottomBackground.addSubview(self.timeLabel)
        self.timeLabel.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
       
        //总时长
        self.durationView.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 0.5)
        self.bottomBackground.addSubview(self.durationView)
        
        //进度
        self.program.backgroundColor = UIColor(red: 84/255.0 , green: 137/255.0, blue: 233/255.0, alpha: 1)
        self.bottomBackground.addSubview(self.program)
    
        self.tempTime = 0
        self.duration = 0
    }
    //进入前台时调用
    @objc func onUIApplicationWillEnterForegroundNotification()
    {
        if(self.status == TalkfunMultiMediaStatusChangeListenerPlaying){
            self.rotateView(self.mp3Image)
        }
    }
   @objc func doubleTapAction()
    {
        if (self.doubleTapActionBlock != nil) {
            self.doubleTapActionBlock!()
        }
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.bottomBackground.frame = CGRect(x: 0, y: self.playVIew.frame.size.height-30, width: self.playVIew.frame.size.width, height: 30)
      
        let mp3BackgroundW:CGFloat = 22
        let mp3BackgroundH:CGFloat = 22

        self.mp3Image.frame = CGRect(x: 15, y: (self.bottomBackground.frame.size.height - mp3BackgroundH)/2, width: mp3BackgroundW, height: mp3BackgroundH)
        
      
        let attrsSize: CGSize = "99:99:99/99:99:99".size(withAttributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14.0)])
        
        self.timeLabel.frame = CGRect(x: self.bottomBackground.frame.size.width - attrsSize.width - 15, y: 0, width: attrsSize.width, height: self.bottomBackground.frame.size.height)

        self.durationView.frame = CGRect(x: 51.5, y: (self.bottomBackground.frame.size.height-3)/2, width: self.bottomBackground.frame.size.width-51.5-attrsSize.width-15-15, height: 3)

    }
    @objc func playStatus(_ status:TalkfunMultiMediaStatusChangeListener, time:NSInteger)

    {
        if (status==TalkfunMultiMediaStatusChangeListenerPlaying) {

            if (self.status != status) {
                //停止动画
                self.mp3Image.layer.removeAllAnimations()
                self.rotateView(self.mp3Image)
            }
         
        }
        else{
                
                 //完成
                 if (status==TalkfunMultiMediaStatusChangeListenerComplate) {
                     //停止动画
                  self.mp3Image.layer.removeAllAnimations()
                   
                  self.program.frame = CGRect(x: 51.5, y: (30-3)/2, width: 0, height: 3)
               
                 
                //关闭了
                 }else if (status==TalkfunMultiMediaStatusChangeListenerClose) {
                     //停止动画
                     self.mp3Image.layer.removeAllAnimations()
                     
                     self.program.frame = CGRect(x: 51.5, y: (30-3)/2, width: 0, height: 3)
                  
                     self.backgroundColor = UIColor.clear

                    self.playVIew.backgroundColor = UIColor.clear
                    self.bottomBackground.isHidden = true
                    self.docType = 0
                }else if (status == TalkfunMultiMediaStatusChangeListenerStop)
                 {
                    //停止动画
                    self.mp3Image.layer.removeAllAnimations()
                }
               
            
        }
        
        var timeStr:String = TalkfunSharedTools.getTimeStr(CGFloat(time)) as String
        
        timeStr = (timeStr as String)+("/" )+(TalkfunSharedTools.getTimeStr(CGFloat(self.duration)) as String)
         self.timeLabel.text = timeStr as String
         self.status = status

         self.tempTime = CGFloat(time)
        
     
        if (self.tempTime>0 && self.duration>0) {
            self.program.frame = CGRect(x: 51.5, y: self.durationView.frame.origin.y, width: self.tempTime/CGFloat(self.duration) * self.durationView.frame.size.width, height: 3)
           
        }
    }
    //类型  (mp3   mp4)            总时长
    @objc func docType(_ type:Int ,duration:Int)
  
    {
        self.docType = type
        
        self.backgroundColor = UIColor.clear
        //音频
        if (type==5) {
             self.playVIew.backgroundColor = UIColor.clear
            
             self.mp3Image.image = UIImage.init(named: "HTLive.bundle/desktopMusic")
        }else{
            self.playVIew.backgroundColor = UIColor.black
            //视频
           
            self.mp3Image.image = UIImage.init(named: "HTLive.bundle/desktopVideo")
            
        }
        self.duration = duration;
        self.mp3Image.layer.removeAllAnimations()
        //停止动画
      
        
        self.bottomBackground.isHidden = false
    
        var timeStr:String = TalkfunSharedTools.getTimeStr(CGFloat(self.tempTime)) as String
        
        timeStr = (timeStr as String)+("/" )+(TalkfunSharedTools.getTimeStr(CGFloat(duration)) as String)
         self.timeLabel.text = timeStr as String

          if (self.tempTime>0&&self.duration>0) {
              self.program.frame = CGRect(x: 51.5, y: self.durationView.frame.origin.y, width: self.tempTime/CGFloat(self.duration) * self.durationView.frame.size.width, height: 3)
            
         self.tempTime = 0;
          }
    }
    func rotateView(_ view:UIImageView)
    {
     view.layer.removeAllAnimations()
     let toValue:CGFloat = .pi*2
     self.rotationAnimation.toValue = toValue
     self.rotationAnimation.duration = 2
        //无限次
     self.rotationAnimation.repeatCount = MAXFLOAT
     view.layer.add(self.rotationAnimation, forKey: "rotationAnimation")
    }

    @objc func removeObserver()
    {
        NotificationCenter.default.removeObserver(self)
      
    }

}
